#include "modeupdate.h"
#include <osgManipulator/Dragger>
#include <osg/Switch>
ModeUpdate::ModeUpdate(QObject *parent)
	: QObject(parent),_currentMode(-1),_previousMode(-1)
{
	
}

ModeUpdate::~ModeUpdate()
{
	
}

void ModeUpdate::recevieMode(int mode)
{
	OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
	_currentMode = mode;
}

void ModeUpdate::operator ()(osg::Node* node, osg::NodeVisitor* nv)
{
	if(nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
	{
		//OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);

		if (_previousMode!=_currentMode)
		{
			osg::Switch* _root = dynamic_cast<osg::Switch*>(node);
			if(_root) 
			{
				if(_previousMode!=-1)
					_root->setValue(_previousMode, false);

				_root->setValue(_currentMode, true);
				//osgManipulator::Dragger* previous = 
				//	(osgManipulator::Dragger*)(_root->getChild(_previousMode));
				//osgManipulator::Dragger* current = 
				//	(osgManipulator::Dragger*)(_root->getChild(_currentMode));
				//
				//if(previous)
				//{
				//	
				//}
				_previousMode = _currentMode;
			}
		}
	}

	traverse(node,nv);
}